//
//  TokenVC.m
//  MyStock
//
//  Created by 刘翔 on 2024/8/18.
//

#import "TokenVC.h"

@interface TokenVC ()
@property (weak, nonatomic) IBOutlet UITextField *userNameF;
@property (weak, nonatomic) IBOutlet UITextField *passF;

@end

@implementation TokenVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.customNavBar.title = @"Token";
}

- (IBAction)getToken:(id)sender {
    
    if (self.userNameF.text.length ==0) {
        [MBProgressHUD showError:@"请输入手机号/账号" toView:self.view];
        return;
    }
    
    if (self.userNameF.text.length !=11 ) {
        [MBProgressHUD showError:@"请输入正确的手机号码" toView:self.view];
        return;
    }
    
    if (self.passF.text.length ==0) {
        [MBProgressHUD showError:@"请输入密码" toView:self.view];
        return;
    }
    
    //调用注册接口
    BADataEntity *entity = [BADataEntity new];
    entity.urlString = [NSString stringWithFormat:@"%@",LoginUrl];
    entity.needCache = NO;
    entity.parameters = @{@"username":self.userNameF.text,@"password":self.passF.text};
    [MBProgressHUD showHUDAddedTo:lxWindow animated:YES];
    [BANetManager ba_request_POSTWithEntity:entity successBlock:^(id response) {
        NSDictionary *result = response;
        if ([result[@"code"] intValue] == 200) {
            
            NSString *token = result[@"data"][@"token"];
            [LxUserDefaults setObject:token forKey:LXToken];
            [LxUserDefaults synchronize];
            [[BANetManager sharedBANetManager] setHttpHeaderFieldDictionary:@{@"token":token}];
           
            [[UIApplication sharedApplication] keyWindow].rootViewController = [[MainTabBarController alloc] init];
            [[[UIApplication sharedApplication] keyWindow] makeKeyWindow];
        }
        
    } failureBlock:^(NSError *error) {
        
        
    } progressBlock:^(int64_t bytesProgress, int64_t totalBytesProgress) {
        
    }];
    
    
  
}


@end
